%Variable Declaration

clear


%Initial parameters

pi_g=0;
pi_b=-1;
mu=[0.1,0.5,0.9];
MGrid=50;

lambda=[0.02,0.04];
sigma=2;        % Tsallis and Renyi parameter
scale=2;        % Multiplication for prop 3
lambda=[lambda,lambda(1)*scale,lambda(2)*scale];

Num_lambda=size(lambda,2);
Num_mu=size(mu,2);


set(gca,'FontSize',20)



%%%%%% GRAPHS in paper %%%%%%%%%%%%%%%%%%%



%*****************************************
%Tsallis high sigma

lambda=[0.2,0.4];
sigma=2;        % Tsallis and Renyi parameter
scale=2;        % Multiplication for prop 3
lambda=[lambda,lambda(1)*scale,lambda(2)*scale];

prtc_ts=zeros(MGrid+1,Num_lambda,Num_mu);


A=[];
b=[];
Aeq=[];
beq=[];
lb=[0,0];
ub=[1,1];
x0=[0.5,0.5];
nonlcon=[];
options=optimset('Algorithm','interior-point');


for k=1:1:Num_mu
    for j=1:1:Num_lambda
        for i=0:1:MGrid
            m=i*((pi_g-pi_b)/MGrid);
            optts=@(x)profit_Ts(pi_g,pi_b,m,mu(k),x(1),x(2),lambda(j),sigma);
            x=fmincon(optts,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
            prtc_ts(i+1,j,k)=mu(k)*x(1)+(1-mu(k))*x(2);
            if prtc_ts(i+1,j,k)<10^(-3)
                prtc_ts(i+1,j,k)=0;
            end

        end
    end
    
    frac_htype=prtc_ts(:,2,k)./(prtc_ts(:,1,k)+prtc_ts(:,2,k));
    X=0:(pi_g-pi_b)/MGrid:(pi_g-pi_b);
    L=plot(X,prtc_ts(:,1,k),'r',X,prtc_ts(:,2,k),'b--',X,frac_htype,'k');
    set(L(1),'LineWidth',2);
    set(L(2),'LineWidth',2);
    set(L(3),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Participation probability');
    plotname1=['\lambda = ' num2str(lambda(1))];
    plotname2=['\lambda = ' num2str(lambda(2))];
    legend(plotname1,plotname2,'P(cost = high | participate)');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    prior=mu(k)*100;
    filename=['../graphs/supplytsallis' num2str(prior) 'sigma' num2str(sigma) '.pdf'];
    saveas(gcf,filename); 
    
    frac_htype2=prtc_ts(:,4,k)./(prtc_ts(:,3,k)+prtc_ts(:,4,k));
    L=plot(X,frac_htype,'k',X,frac_htype2,'k-.');
    set(L(1),'LineWidth',4);
    set(L(2),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Fraction of High Types');
    legend('Initial','Scaled');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    filename=['../graphs/scaletsallis' num2str(prior) 'sigma' num2str(sigma) '.pdf'];
    saveas(gcf,filename); 
    
end
    

%*****************************************
%Renyi


lambda=[0.2,0.4];
sigma=2;      % Tsallis and Renyi parameter
scale=2;        % Multiplication for prop 3
lambda=[lambda,lambda(1)*scale,lambda(2)*scale];


prtc_re=zeros(MGrid+1,Num_lambda,Num_mu);

A=[];
b=[];
Aeq=[];
beq=[];
lb=[0,0];
ub=[1,1];
x0=[0,0];
nonlcon=[];
options=optimset('Algorithm','interior-point');

for k=1:1:Num_mu
    for j=1:1:Num_lambda
        for i=0:1:MGrid
            m=i*((pi_g-pi_b)/MGrid);
            optre=@(x)profit_Re(pi_g,pi_b,m,mu(k),x(1),x(2),lambda(j),sigma);
            x=fmincon(optre,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
            prtc_re(i+1,j,k)=mu(k)*x(1)+(1-mu(k))*x(2);
            if prtc_re(i+1,j,k)<10^(-3)
                prtc_re(i+1,j,k)=0;
            end

        end
    end
    
    frac_htype=prtc_re(:,2,k)./(prtc_re(:,1,k)+prtc_re(:,2,k));
    X=0:(pi_g-pi_b)/MGrid:(pi_g-pi_b);
    L=plot(X,prtc_re(:,1,k),'r',X,prtc_re(:,2,k),'b--',X,frac_htype,'k');
    set(L(1),'LineWidth',2);
    set(L(2),'LineWidth',2);
    set(L(3),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Participation probability');
    plotname1=['\lambda = ' num2str(lambda(1))];
    plotname2=['\lambda = ' num2str(lambda(2))];
    legend(plotname1,plotname2,'P(cost = high | participate)');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    prior=mu(k)*100;
    filename=['../graphs/supplyrenyi' num2str(prior) '.pdf'];
    saveas(gcf,filename); 
    
    frac_htype2=prtc_re(:,4,k)./(prtc_re(:,3,k)+prtc_re(:,4,k));
    L=plot(X,frac_htype,'k',X,frac_htype2,'k-.');
    set(L(1),'LineWidth',4);
    set(L(2),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Fraction of High Types');
    legend('Initial','Scaled');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    filename=['../graphs/scalerenyi' num2str(prior) '.pdf'];
    saveas(gcf,filename); 
    
end


%*****************************************
%Tsallis low sigma


lambda=[0.3,0.6];
sigma=0.1;      % Tsallis and Renyi parameter
scale=2;        % Multiplication for prop 3
lambda=[lambda,lambda(1)*scale,lambda(2)*scale];

prtc_ts=zeros(MGrid+1,Num_lambda,Num_mu);


A=[];
b=[];
Aeq=[];
beq=[];
lb=[0,0];
ub=[1,1];
x0=[0.5,0.5];
nonlcon=[];
options=optimset('Algorithm','interior-point');


for k=1:1:Num_mu
    for j=1:1:Num_lambda
        for i=0:1:MGrid
            m=i*((pi_g-pi_b)/MGrid);
            optts=@(x)profit_Ts(pi_g,pi_b,m,mu(k),x(1),x(2),lambda(j),sigma);
            x=fmincon(optts,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
            prtc_ts(i+1,j,k)=mu(k)*x(1)+(1-mu(k))*x(2);
            if prtc_ts(i+1,j,k)<10^(-3)
                prtc_ts(i+1,j,k)=0;
            end

        end
    end
    
    frac_htype=prtc_ts(:,2,k)./(prtc_ts(:,1,k)+prtc_ts(:,2,k));
    X=0:(pi_g-pi_b)/MGrid:(pi_g-pi_b);
    L=plot(X,prtc_ts(:,1,k),'r',X,prtc_ts(:,2,k),'b--',X,frac_htype,'k');
    set(L(1),'LineWidth',2);
    set(L(2),'LineWidth',2);
    set(L(3),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Participation probability');
    plotname1=['\lambda = ' num2str(lambda(1))];
    plotname2=['\lambda = ' num2str(lambda(2))];
    legend(plotname1,plotname2,'P(cost = high | participate)');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    prior=mu(k)*100;
    filename=['../graphs/supplytsallis' num2str(prior) 'sigma' num2str(sigma) '.pdf'];
    saveas(gcf,filename); 
    
    frac_htype2=prtc_ts(:,4,k)./(prtc_ts(:,3,k)+prtc_ts(:,4,k));
    L=plot(X,frac_htype,'k',X,frac_htype2,'k-.');
    set(L(1),'LineWidth',4);
    set(L(2),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Fraction of High Types');
    legend('Initial','Scaled');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    filename=['../graphs/scaletsallis' num2str(prior) 'sigma' num2str(sigma) '.pdf'];
    saveas(gcf,filename); 
    
end


%*****************************************
%Logit


lambda=[0.025,0.05];
sigma=0.2;      % Tsallis and Renyi parameter
scale=2;        % Multiplication for prop 3
lambda=[lambda,lambda(1)*scale,lambda(2)*scale];

prtc_lg=zeros(MGrid+1,Num_lambda,Num_mu);


A=[];
b=[];
Aeq=[];
beq=[];
lb=[0,0];
ub=[1,1];
x0=[0.5,0.5];
nonlcon=[];
options=optimset('Algorithm','interior-point');

for k=1:1:Num_mu
    for j=1:1:Num_lambda
        for i=0:1:MGrid
            m=i*((pi_g-pi_b)/MGrid);
            optlg=@(x)profit_Lg(pi_g,pi_b,m,mu(k),x(1),x(2),lambda(j));
            x=fmincon(optlg,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
            prtc_lg(i+1,j,k)=mu(k)*x(1)+(1-mu(k))*x(2);
            if prtc_lg(i+1,j,k)<10^(-3)
                prtc_lg(i+1,j,k)=0;
            end

        end
    end
    
    frac_htype=prtc_lg(:,2,k)./(prtc_lg(:,1,k)+prtc_lg(:,2,k));
    X=0:(pi_g-pi_b)/MGrid:(pi_g-pi_b);
    L=plot(X,prtc_lg(:,1,k),'r',X,prtc_lg(:,2,k),'b--',X,frac_htype,'k');
    set(L(1),'LineWidth',2);
    set(L(2),'LineWidth',2);
    set(L(3),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Participation probability');
    plotname1=['\lambda = ' num2str(lambda(1))];
    plotname2=['\lambda = ' num2str(lambda(2))];
    legend(plotname1,plotname2,'P(cost = high | participate)');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    prior=mu(k)*100;
    filename=['../graphs/supplylogit' num2str(prior) '.pdf'];
    saveas(gcf,filename); 
    
    frac_htype2=prtc_lg(:,4,k)./(prtc_lg(:,3,k)+prtc_lg(:,4,k));
    L=plot(X,frac_htype,'k',X,frac_htype2,'k-.');
    set(L(1),'LineWidth',4);
    set(L(2),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Fraction of High Types');
    legend('Initial','Scaled');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    filename=['../graphs/scalelogit' num2str(prior) '.pdf'];
    saveas(gcf,filename); 
    
end


%*********************************************************************
%Shannon

lambda=[0.2,0.4];
sigma=0.2;      % Tsallis and Renyi parameter
scale=2;        % Multiplication for prop 3
lambda=[lambda,lambda(1)*scale,lambda(2)*scale];



prtc=zeros(MGrid+1,Num_lambda,Num_mu);


A=[];
b=[];
Aeq=[];
beq=[];
lb=[0,0];
ub=[1,1];
x0=[0.5,0.5];
nonlcon=[];
options=optimset('Algorithm','interior-point');

for k=1:1:Num_mu
    for j=1:1:Num_lambda
        for i=0:1:MGrid
            m=i*((pi_g-pi_b)/MGrid);
            optsh=@(x)profit_Sh(pi_g,pi_b,m,mu(k),x(1),x(2),lambda(j));
            x=fmincon(optsh,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
            prtc(i+1,j,k)=mu(k)*x(1)+(1-mu(k))*x(2);
            if prtc(i+1,j,k)<10^(-3)
                prtc(i+1,j,k)=0;
            end

        end
    end
    
    frac_htype=prtc(:,2,k)./(prtc(:,1,k)+prtc(:,2,k));
    X=0:(pi_g-pi_b)/MGrid:(pi_g-pi_b);
    L=plot(X,prtc(:,1,k),'r' ,X,prtc(:,2,k),'b--',X,frac_htype,'k');
    set(L(1),'LineWidth',2);
    set(L(2),'LineWidth',2);
    set(L(3),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Participation probability');
    plotname1=['\lambda = ' num2str(lambda(1))];
    plotname2=['\lambda = ' num2str(lambda(2))];
    legend(plotname1,plotname2,'P(cost = high | participate)');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    prior=mu(k)*100;
    filename=['../graphs/supplyshannon' num2str(prior) '.pdf'];
    saveas(gcf,filename); 
    
    frac_htype2=prtc(:,4,k)./(prtc(:,3,k)+prtc(:,4,k));
    L=plot(X,frac_htype,'k',X,frac_htype2,'k-.');
    set(L(1),'LineWidth',4);
    set(L(2),'LineWidth',4);
    xlabel('Incentive m');
    ylabel('Fraction of High Types');
    legend('Initial','Scaled');
    if k >= 2
        legend('Location','southeast');
    else
        legend('Location','northwest');
    end
    filename=['../graphs/scaleshannon' num2str(prior) '.pdf'];
    saveas(gcf,filename); 
    
end



%}




